package cn.xd.security.config;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authorization.AuthorizationDecision;
import org.springframework.security.authorization.AuthorizationManager;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.web.access.intercept.RequestAuthorizationContext;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.PathMatcher;

import java.util.List;
import java.util.function.Supplier;

/**
 * @author : mirzaLin
 * @date : 2024/1/28
 * @description :
 **/
@Component
@Slf4j
public class BaseUrlAuthorizationManager implements AuthorizationManager<RequestAuthorizationContext> {
    @Resource
    private SecurityProperties securityProperties;

    @Override
    public AuthorizationDecision check(Supplier<Authentication> authentication, RequestAuthorizationContext requestAuthorizationContext) {
        String requestURI = requestAuthorizationContext.getRequest().getRequestURI();

        List<String> authorities = authentication.get().getAuthorities().stream().map(GrantedAuthority::getAuthority).toList();
        boolean granted = Boolean.TRUE;
//        PathMatcher pathMatcher = new AntPathMatcher();
//        for (String authority : authorities) {
//            granted = granted || pathMatcher.match(authority, requestURI);
//        }
        return new AuthorizationDecision(granted);
    }
}
